package com.yuefresh.sys;

import java.util.*;

public class CachedPathMapper extends PathMapper {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> cacheMap;
	private Map<String, Object> cacheAllMap;

	/**
	 * Creates a CachedPathMapper object that will use cacheMap to cache the
	 * results of the {@link #get(String)} calls and cacheAllMap to cache the
	 * results of the {@link #getAll(String)} class.
	 * 
	 * @param cacheMap
	 * @param cacheAllMap
	 */
	public CachedPathMapper(Map<String, Object> cacheMap,
			Map<String, Object> cacheAllMap) {
		// Synchronize access to the map for muti-threaded access
		this.cacheMap = Collections.synchronizedMap(cacheMap);
		this.cacheAllMap = Collections.synchronizedMap(cacheAllMap);
	}

	public String get(String path) {
		// Check the cache
		if (cacheMap.containsKey(path)) {
			// The result for this path is cached, return the value
			return (String) cacheMap.get(path);
		}
		// Get the result from PathMapper
		final String result = super.get(path);
		// Cache the result
		cacheMap.put(path, result);
		return result;
	}

	@SuppressWarnings("unchecked")
	public Collection<Object> getAll(String path) {
		// Check the cache
		if (cacheAllMap.containsKey(path)) {
			// The result for this key is cached, return the value
			return (Collection<Object>) cacheAllMap.get(path);
		}
		// Get the result from PathMapper
		final Collection<Object> result = super.getAll(path);
		// Cache the result
		cacheAllMap.put(path, result);
		return result;
	}

	public void put(String key, String pattern) {
		// Check if we have the entry in the caches
		if (cacheMap.containsKey(key)) {
			cacheMap.remove(key);
		}
		if (cacheAllMap.containsKey(key)) {
			cacheAllMap.remove(key);
		}
		// Let PathMapper update the patterns
		super.put(key, pattern);
	}

}
